package com.itheima.controller;

import com.itheima.advice.OneAnnotation;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryPaeam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result page(StudentQueryPaeam studentQueryPaeam) {
        PageResult pageResult = studentService.page(studentQueryPaeam);
        return Result.success(pageResult);
    }

    @OneAnnotation
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        studentService.removeByIds(ids);
        return Result.success();
    }

    @OneAnnotation
    @PostMapping
    public Result add(@RequestBody Student student) {
        studentService.saveStu(student);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    @OneAnnotation
    @PutMapping
    public Result update(@RequestBody Student student){
        student.setUpdateTime(LocalDateTime.now());
        studentService.updateById(student);
        return Result.success();
    }

    @OneAnnotation
    @PutMapping("/violation/{id}/{score}")
    public Result score(@PathVariable Integer id ,@PathVariable Integer score) {
        studentService.score(id,score);
        return Result.success();
    }
}
